/*
 * Copyright 1999-2010 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.alibaba.aone.casestudy.napoli;

import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Failure;
import com.alibaba.aone.casestudy.common.entity.FailureRpAsignGroup;
import com.alibaba.aone.casestudy.common.entity.FailureTime;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.aone.casestudy.service.FailureTimeService;
import com.alibaba.aone.casestudy.service.GroupService;

import com.alibaba.citrus.util.StringUtil;
import com.alibaba.napoli.client.async.AsyncWorker;

/**
 * 类NapoliWorker.java的实现描述：TODO 类实现描述 
 * @author gaojian 2011-5-4 下午01:26:30
 */
public class NapoliWorker implements AsyncWorker{

	@Autowired 
	private FailureService failureService;
	@Autowired
	private GroupService groupService;
	
	@Autowired
	private FailureTimeService failureTimeService;
    
    private Logger              logger     = LoggerFactory.getLogger(NapoliWorker.class);

    /* (non-Javadoc)
     * @see com.alibaba.napoli.client.async.AsyncWorker#doWork(java.io.Serializable)
     */
    public boolean doWork(Serializable msg) {       
        try {
        	Map failureMap = (Map)msg;
        	if(failureMap == null)
        		return false;
        	Boolean flag = (Boolean) failureMap.get("isFailureMsg");
        	if(flag == null || !flag)
        		return false;
			return doAddFailure(failureMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("received msg error", e);
			return false;
		}
    }
    
    private boolean doAddFailure(Map msg) throws ParseException {
        logger.info("received msg");
        Map failureMap = (Map)msg;
        
    	String title = (String) failureMap.get("title");
		String number = (String) failureMap.get("number");
		String defini = (String) failureMap.get("defini");
		String registrant = (String) failureMap.get("registrant");
		Integer discoveryMethod = (Integer) failureMap.get("discoveryMethod"); //((String) failureMap.get("discoveryMethod")==null) ? 0:Integer.parseInt((String) failureMap.get("discoveryMethod"));
		String isReasonClear = (String) failureMap.get("isReasonClear");
		Double score = (Double) failureMap.get("score");
		String correlationDepartment = (String) failureMap.get("correlationDepartment");
		String description = (String) failureMap.get("description");
		String processingRecord = (String) failureMap.get("processingRecord");
		
		
		String rpDepartment = (String) failureMap.get("rpDepartment");
		String rpProductLine = (String) failureMap.get("rpProductLine");
		String rpMan = (String) failureMap.get("rpMan");
		String rpRole = (String) failureMap.get("rpRole");
		String rpManager = (String) failureMap.get("rpManager");
		String rpAccount = (String) failureMap.get("rpAccount");
		
		String gmtStart1 = (String) failureMap.get("gmtStart1");
		String gmtEnd1 = (String) failureMap.get("gmtEnd1");
    	
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		
		Date gmtStart = StringUtil.isEmpty(gmtStart1)? null:format.parse(gmtStart1);
		
		Date gmtEnd =  StringUtil.isEmpty(gmtEnd1)? null:format.parse(gmtEnd1);
		
		//此处需要重构
		int rank = ((String) failureMap.get("rank")==null )? 0:Integer.parseInt((String) failureMap.get("rank"));
		
    	Failure failure = new Failure();
		failure.setTitle(title);
		failure.setNumber(number);
		failure.setDefini(defini);
		failure.setRegistrant(registrant);
		failure.setDiscoveryMethod(discoveryMethod);
		failure.setIsReasonClear(isReasonClear);
		failure.setScore(score);
		failure.setCorrelationDepa(correlationDepartment);
		failure.setDescription(description);
		failure.setProcessingRecord(processingRecord);	
		
		
		//判断数据库中是否已经存在
		if(null==failureService.getFailureByNumber(number)){
		failureService.addFailure(failure);
		
		FailureRpAsignGroup group = new FailureRpAsignGroup();
		
		group.setFailureNumber(number);
		group.setRpRole(rpRole);
		group.setRpProductLine(rpProductLine);
		group.setRpManager(rpManager);
		group.setRpMan(rpMan);
		group.setRpDepartment(rpDepartment);
		
		groupService.add(group);
		
		
		
		FailureTime failureTime = new FailureTime();
		failureTime.setFailureNumber(number);
		if(gmtStart!=null){
			failureTime.setGmtStart(gmtStart);
		}
		if(gmtEnd!=null){
			failureTime.setGmtEnd(gmtEnd);
		}
		
		failureTime.setRank(rank);
		
		failureTimeService.add(failureTime);
		}
		
		System.out.println("<------------------------------------------------------------------------------->");
		System.out.println(isReasonClear);
		System.out.println(discoveryMethod);
		System.out.println(processingRecord);
		System.out.println(description);
		System.out.println("<====================================>");
		System.out.println(failure.getDescription());
		System.out.println(failure.getProcessingRecord());
		System.out.println("<====================================>");
		System.out.println(rpDepartment);
		System.out.println("<------------------------------------------------------------------------------->");
		
	
		return true;
		
		
		
    }

}
